<?php
require_once 'fun.php';
require_once 'push.php';
$param = file_get_contents('php://input');
file_put_contents('refund.txt', $param);
$param = json_decode($param, true);
$out_refund_no = 'tk' . uniqid();
$data = [
    "app_id" => $param['app_id'],
    "out_order_no" => $param['out_order_no'],
    "out_refund_no" => $out_refund_no,
    "reason" => $param['reason'],
    "refund_amount" => (int) $param['refund_amount'],
    // "cp_extra" => $param['cp_extra'],
    // "notify_url" => $param['notify_url'],
    "disable_msg" => 1,
    // "msg_page" => $param['msg_page']
];
// $data['notify_url'] = '';
// var_dump($data);exit;
$res = GetApi("https://developer.toutiao.com/api/apps/ecpay/v1/create_refund", $data);
$res = json_decode($res, true);
if ($res['err_no'] == 0) {
    $res_refund = GetApi("https://developer.toutiao.com/api/apps/ecpay/v1/query_refund", [
        "app_id" => $param['app_id'],
        "out_refund_no" => $out_refund_no,
    ]);
   
    $res_refund = json_decode($res_refund, true);

    if ($res_refund['err_no'] == 0) {

        $query_order = GetApi("https://developer.toutiao.com/api/apps/ecpay/v1/query_order", [
            "app_id" => $param['app_id'],
            "out_order_no" => $param['out_order_no'],
        ]);
        file_put_contents('refundinfo.txt', $query_order);
        $query_order = json_decode($query_order, true);
        if (empty($query_order['payment_info']['cp_extra'])) {
            echo json_encode([
                'code' => 0,
                'msg' => "订单信息不能为空"
            ]);
            exit;
        }
        $cp_extra = strToData($query_order['payment_info']['cp_extra']);
        $cp = getPush([
            "app_name" => "douyin",
            "open_id" => $cp_extra['open_id'],
            "order_detail" => json_encode([
                "order_id" => $param['out_order_no'],
                // "order_id" => $param['out_order_no'],
                "create_time" => (int) $cp_extra['create_time'],  //订单创建时间
                "status" => "已退款",
                "amount" => 1,
                "total_price" => (float) $param['refund_amount'],
                "detail_url" => $cp_extra['detail_url'],
                "item_list" => [
                    [
                        "item_code" => $cp_extra['item_code'],
                        "img" => $cp_extra['img'],
                        "title" => $cp_extra['title'],
                        "sub_title" => $cp_extra['sub_title'], //子订单商品介绍副标题
                        "amount" => 1,
                        "price" => (float) $param['refund_amount']
                    ]
                ]
            ]),
            "order_status" => 6,
            "order_type" => 0,
            "update_time" => time(),

        ]);
        $cp = json_decode($cp, true);
        if ($cp['err_code'] == 0) {
            echo json_encode([
                'code' => 1,
                'msg' => 'ok'
            ]);
            exit;
        } else {
            echo json_encode([
                'code' => 0,
                'err_msg' => $res['err_msg']
            ]);
            exit;
        }

    } else {
        echo json_encode([
            'code' => 0,
            'err_tips' => $res['err_tips']
        ]);
        exit;
    }

} else {
    echo json_encode([
        'code' => 0,
        'msg' => $res['err_tips']
    ]);
    exit;
}